//
//  VisitorLoginView.swift
//  超杰的新浪微博
//
//  Created by mac on 15/8/4.
//  Copyright © 2015年 ios. All rights reserved.
//

import UIKit

//代理协议要定义在外面

protocol VisitorLoginViewDelegate: NSObjectProtocol{
    func visitorloginViewlogin()

    func visitorLoginViewRegister()
}

class VisitorLoginView: UIView {
    
    //定义代理
    weak var delegate : VisitorLoginViewDelegate?
    
    func clickLogin(){
        
        delegate?.visitorloginViewlogin()
    
        print("登录")
    }
    func clickregister(){
    
        delegate?.visitorLoginViewRegister()
        print("注册")
    }
    
    //home的核心动画
    
    private func startAnimation(){
        //创建一个核心动画
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        //动画重复的次数
        anim.repeatCount = MAXFLOAT
        //动画旋转的角度
        anim.toValue = 2 * M_PI
        //动画的持续时间
        anim.duration = 20.0
        //禁止溢出动画
        anim.removedOnCompletion = false
        //将控件添加核心动画
        iconView.layer.addAnimation(anim, forKey: nil)
    }
    //重写类的构造方法，在类创建的时候调用
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    //storyboard创建类时候调用
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        
        setupUI()
    }
    
    //设置视图信息
    func setupViewinfo(isHome : Bool, imageName: String, message: String){
    
        messageLable.text = message
        
        iconView.image = UIImage(named: imageName)
        
        homeIconView.hidden = !isHome
        //第一种方法可以隐藏，也可以让视图到最后面去
//        maskIconVIew.hidden = !isHome
        //第二种方法
        
        isHome ? startAnimation() :  sendSubviewToBack(maskIconVIew)
        
        
    }
    
    //添加控件
    private func setupUI(){
        
        addSubview(iconView)
        addSubview(maskIconVIew)
        addSubview(homeIconView)
        addSubview(messageLable)
        addSubview(regesterBtton)
        addSubview(loginButton)
        print("控件创建")
   
        // 2. 自动布局
        // 默认在纯代码开发时，是可以直接使用setFrame设置控件位置的
        // 如果要使用自动布局，需要设置一个属性
        // 一旦加入自动布局系统管理，就不要再设置frame
        // 1> 图标
        iconView.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: -40))
        
       
        
        // 2> 小房子 - 用代码开发时，最好所有相关的控件最好有一个统一的参照物
        homeIconView.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: homeIconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: homeIconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: 0))
        
        // 3> 消息文字
        messageLable.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: messageLable, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: messageLable, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: messageLable, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 224))
        
        // 4> 注册按钮
        regesterBtton.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: regesterBtton, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: messageLable, attribute: NSLayoutAttribute.Left, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: regesterBtton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLable, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: regesterBtton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: regesterBtton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 35))
        
        // 5> 登录按钮
        loginButton.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: messageLable, attribute: NSLayoutAttribute.Right, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLable, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 35))
        
        // 6> 遮罩视图 VFL 可视化格式语言
        // 美工提供的素材，大多是纯色的
        maskIconVIew.translatesAutoresizingMaskIntoConstraints = false
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[subview]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["subview" : maskIconVIew]))
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[subview]-(-35)-[regester]", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["subview": maskIconVIew, "regester": regesterBtton]))
        
        //设置背景颜色
        backgroundColor = UIColor(white: 237 / 255, alpha: 1)
        
        //遮罩视图的自动布局
//        maskIconVIew.translatesAutoresizingMaskIntoConstraints = false
//        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[subview]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["subview" : maskIconVIew]))
//        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("v:|-0-[subview]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["subview" : maskIconVIew]))
        
    }
    
    
    
    
    private lazy var iconView: UIImageView = {
    
        let iv = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
        
        
        return iv
    }()
    
    //遮罩视图
    private lazy var maskIconVIew: UIImageView = {
        
        let iv = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
        
        return iv
        }()
    
    private lazy var homeIconView: UIImageView = {
        let iv = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
        
        return iv
        }()
    
    private lazy var messageLable: UILabel = {
        
        let lable = UILabel()
        
        lable.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        lable.sizeToFit()
        lable.font = UIFont.systemFontOfSize(14)
        lable.numberOfLines = 0
        return lable
        }()

    private lazy var regesterBtton: UIButton = {
    
        let btn = UIButton()
        
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        
        btn.setTitle("注册", forState: UIControlState.Normal)
        
        btn.addTarget(self, action: "clickregister", forControlEvents: UIControlEvents.TouchUpInside)
        
        return btn
        
    }()
    
   
    
    private lazy var loginButton : UIButton = {
        
        let btn = UIButton()
        
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        btn.setTitle("登录", forState: UIControlState.Normal)
        
        btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        
        btn.addTarget(self, action: "clickLogin", forControlEvents: UIControlEvents.TouchUpInside)
        
        return btn
        
    }()

   
    

}
